home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2131 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: news.cencom.net!ns!tanp
  2. From: tanp@ns (Bill Wendling)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help ?!
  5. Date: 19 Jan 1996 05:21:56 GMT
  6. Organization: Cen-Com Internet
  7. Message-ID: <4dn9pk$43o@news.cencom.net>
  8. References: <4dm889$3hs@neptunus.pi.net>
  9. NNTP-Posting-Host: ns.cencom.net
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. mv@pi.net inexplicably wrote:
  13. } Hello everybody,
  14.  
  15.  
  16. } Please take a look at this:
  17.  
  18. } ----------------------------
  19. } #include <stdio.h>
  20. } #include <stdlib.h>
  21. } #include <conio.h>
  22. } #include <string.h>
  23. } #include <alloc.h>
  24.  
  25.  
  26. } #define MAXLEN    20
  27. } #define MAXSWITCH 20
  28.  
  29.  
  30. } char *s="dir /w:test/1/2/3/4";
  31. } char *s2, *s3;
  32. } char myswitches[MAXSWITCH][MAXLEN];
  33.  
  34.  
  35. } int get_switches(char *cmd, char *switches[][MAXLEN])
  36.  
  37. /*
  38. ACK!!!  You didn't pass what you thought you passed.  
  39.     char *switches[][MAXLEN] 
  40. is a pointer to a double array...a 3 dimensional array!
  41. You should define it as:
  42.     char *switches[MAXLEN]
  43. This will help greatly.
  44. */
  45.  
  46.  
  47. } {
  48. }  int i=0;
  49. }  char *cpy;
  50. }  char *saved;
  51.  
  52. }  if (strlen(cmd)==0)
  53. }      return 0;
  54. }  
  55. }  cpy=(char *)malloc(MAXLEN);
  56.  
  57. }  if (!cpy)
  58. }  {
  59. }      printf("Out of memory in %s on line: %d\n",__FILE__,__LINE__);
  60. }      exit(EXIT_FAILURE);
  61. }  }
  62.  
  63. }  strcpy(cpy,'\0');
  64.  
  65. /* 
  66. This should be:
  67.     strcpy(cpy, "\0");
  68. since it expects a char * in the second parm
  69.  
  70. Better yet, you could just say:
  71.     cpy[0] = '\0';
  72. or
  73.     *cpy = '\0';
  74. */
  75.  
  76. }  saved=cpy;
  77.  
  78. }  while (*cmd!='\0')
  79. }  {
  80. }      if (*cmd=='/')
  81. }       {
  82. }           if (i==MAXSWITCH)
  83.  
  84. /*
  85. Better is:
  86.         if (i >= MAXSWITCH)
  87. since it will assure you that if i goes over MAXSWITCH, it will do this.
  88. */
  89.  
  90. }            {
  91. }                 printf("INT: Too many switches ...\n");
  92. }                 return 9999;
  93. }            }
  94.  
  95. }           *cpy=*cmd;
  96. }           do { *cpy++=*cmd++; } while (!strchr(" /-\0",*cmd));
  97.  
  98. }            *cpy='\0';
  99. }              cpy=saved;
  100. }               strcpy(switches[i],cpy);
  101. }             strcpy(cpy,'\0');
  102.  
  103. /* 
  104.         strcpy(cpy, "\0");
  105. */
  106.  
  107. }            i++;
  108. }          }
  109.  
  110. /*
  111. I:
  112.  
  113. Put and "else" here (see II at bottom).
  114. */
  115.  
  116. }         if (*cmd=='-')
  117. }         {
  118. }              if (i==MAXSWITCH)
  119.  
  120. /*
  121. Again:
  122.         if (i >= MAXSWITCH)
  123. */
  124.  
  125. }            {
  126. }                 printf("INT: Too many switches ...\n");
  127. }                 return 9999;
  128. }            }
  129.  
  130. }              *cpy=*cmd;
  131. }           do { *cpy++=*cmd++; } while (!strchr(" /-\0",*cmd));
  132. }           *cpy='\0';
  133. }             cpy=saved;
  134. }              strcpy(switches[i],cpy);
  135. }            strcpy(cpy,'\0');
  136.  
  137. /* 
  138.         strcpy(cpy, "\0");
  139. */
  140.  
  141. }            i++;
  142. }          }
  143.  
  144. /*
  145. II:
  146.  
  147. Put the code here:
  148.     else
  149.         cmd++;
  150.  
  151. here.
  152. */
  153.  
  154.  
  155. }      if (!strchr("/-\0",*cmd)) 
  156.         *cmd++;
  157.  
  158. /*
  159.         *cmd++;
  160. is incrementing what cmd points to at that time...Not good...See above
  161. else's for a good way to increment through the string.
  162. */
  163. }  }
  164.  
  165. }  free(cpy);
  166. }  return i;
  167. } }
  168.  
  169.  
  170. } int main (void)
  171.  
  172.  
  173. } {
  174. }     int i;
  175.  
  176. }     clrscr();
  177. }       i=get_switches(s,myswitches);
  178. }        printf("Switches found: %i\n",i);
  179. }        for (i=0; i<MAXSWITCH; i++)
  180. }         {
  181.  
  182.  
  183. } /*
  184.  
  185. }  Here it should print:
  186.  
  187. } Switch: /w:test
  188. } Switch: /1
  189. } etc..
  190.  
  191. } but it prints:
  192.  
  193. } Switch: /w:test
  194. } Switch: 
  195. } Switch: /1
  196. } and so on....
  197. } It jjust skips one array index everytime... It seems to go wrong in get_switches()...
  198. } */
  199. }              printf("Switch: %s\n",myswitches[i]);
  200. }         }
  201. }      return 0;
  202. } }
  203.  
  204. Other than those things I mentioned, it looks alright.  The 
  205. char *switches[][MAXLEN] problem may be the real culprit.
  206.  
  207. } TTYL,
  208.  
  209. } Martijn....
  210.  
  211.  
  212. --
  213. Bill Wendling         | "Pinky, are you thinking what I'm thinking?"
  214. tanp@ns.cencom.net  | "I think so, Brain, but burlap chafes me so."
  215. "Boom Shanka"       | Finger me for my Geek Code...NOW!
  216.